﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;

namespace YArchitech.Plumbing
{
	internal class FireHydantCalUserInfoReadandWrite
	{
		public FireHydantCalUserInfoReadandWrite(string path)
		{
			this.path = path;
		}

		public void creat(string rootNode)
		{
			if (!File.Exists(this.path))
			{
				XmlDocument xmlDocument = new XmlDocument();
				XmlDeclaration newChild = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
				xmlDocument.AppendChild(newChild);
				XmlElement xmlElement = xmlDocument.CreateElement(rootNode);
				xmlDocument.AppendChild(xmlElement);
				XmlNode xmlNode = xmlDocument.CreateElement("add");
				(xmlNode as XmlElement).SetAttribute("name", "默认");
				(xmlNode as XmlElement).SetAttribute("comboBoxNozzleDiameter", "16");
				(xmlNode as XmlElement).SetAttribute("comboBoxFireDiameter", "50");
				(xmlNode as XmlElement).SetAttribute("comboBoxFireMaterial", "麻织");
				(xmlNode as XmlElement).SetAttribute("comboBoxFireLength", "20");
				(xmlNode as XmlElement).SetAttribute("comboBoxNozzleAngle", "45");
				(xmlNode as XmlElement).SetAttribute("checkBox", "1");
				(xmlNode as XmlElement).SetAttribute("textBoxWaterLevels", "5");
				xmlElement.AppendChild(xmlNode);
				xmlDocument.Save(this.path);
			}
		}

		public FireHydantStruct readMoRen(string rootNode)
		{
			FireHydantStruct fireHydantStruct = new FireHydantStruct();
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(this.path);
			foreach (object obj in xmlDocument.SelectSingleNode(rootNode).ChildNodes)
			{
				XmlElement xmlElement = (XmlElement)((XmlNode)obj);
				if (xmlElement.Attributes["name"].Value == "默认")
				{
					fireHydantStruct.FireName = xmlElement.Attributes["name"].Value;
					fireHydantStruct.ComboBoxNozzleDiameter = xmlElement.Attributes["comboBoxNozzleDiameter"].Value;
					fireHydantStruct.ComboBoxFireDiameter = xmlElement.Attributes["comboBoxFireDiameter"].Value;
					fireHydantStruct.ComboBoxFireMaterial = xmlElement.Attributes["comboBoxFireMaterial"].Value;
					fireHydantStruct.ComboBoxFireLength = xmlElement.Attributes["comboBoxFireLength"].Value;
					fireHydantStruct.ComboBoxNozzleAngle = xmlElement.Attributes["comboBoxNozzleAngle"].Value;
					fireHydantStruct.CheckBox = xmlElement.Attributes["checkBox"].Value;
					fireHydantStruct.TextBoxWaterLevels = xmlElement.Attributes["textBoxWaterLevels"].Value;
				}
			}
			return fireHydantStruct;
		}

		public List<FireHydantStruct> readXML(string rootNode)
		{
			List<FireHydantStruct> list = new List<FireHydantStruct>();
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(this.path);
			foreach (object obj in xmlDocument.SelectSingleNode(rootNode).ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				FireHydantStruct fireHydantStruct = new FireHydantStruct();
				XmlElement xmlElement = (XmlElement)xmlNode;
				fireHydantStruct.FireName = xmlElement.Attributes["name"].Value;
				fireHydantStruct.ComboBoxNozzleDiameter = xmlElement.Attributes["comboBoxNozzleDiameter"].Value;
				fireHydantStruct.ComboBoxFireDiameter = xmlElement.Attributes["comboBoxFireDiameter"].Value;
				fireHydantStruct.ComboBoxFireMaterial = xmlElement.Attributes["comboBoxFireMaterial"].Value;
				fireHydantStruct.ComboBoxFireLength = xmlElement.Attributes["comboBoxFireLength"].Value;
				fireHydantStruct.ComboBoxNozzleAngle = xmlElement.Attributes["comboBoxNozzleAngle"].Value;
				fireHydantStruct.CheckBox = xmlElement.Attributes["checkBox"].Value;
				fireHydantStruct.TextBoxWaterLevels = xmlElement.Attributes["textBoxWaterLevels"].Value;
				list.Add(fireHydantStruct);
			}
			return list;
		}

		public void writeXML(List<FireHydantStruct> list, string rootNode)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(this.path);
			XmlNode xmlNode = xmlDocument.SelectSingleNode(rootNode);
			XmlNodeList childNodes = xmlDocument.SelectSingleNode(rootNode).ChildNodes;
			xmlNode.RemoveAll();
			list.Add(new FireHydantStruct
			{
				FireName = "默认",
				ComboBoxNozzleDiameter = "16",
				ComboBoxFireDiameter = "50",
				ComboBoxFireMaterial = "麻织",
				ComboBoxFireLength = "20",
				ComboBoxNozzleAngle = "45",
				CheckBox = "1",
				TextBoxWaterLevels = "5"
			});
			for (int i = 0; i < list.Count; i++)
			{
				for (int j = i + 1; j < list.Count; j++)
				{
					if (list[i].FireName == list[j].FireName)
					{
						list.RemoveAt(j);
						j--;
					}
				}
			}
			foreach (FireHydantStruct fireHydantStruct in list)
			{
				XmlNode xmlNode2 = xmlDocument.CreateElement("add");
				(xmlNode2 as XmlElement).SetAttribute("name", fireHydantStruct.FireName);
				(xmlNode2 as XmlElement).SetAttribute("comboBoxNozzleDiameter", fireHydantStruct.ComboBoxNozzleDiameter);
				(xmlNode2 as XmlElement).SetAttribute("comboBoxFireDiameter", fireHydantStruct.ComboBoxFireDiameter);
				(xmlNode2 as XmlElement).SetAttribute("comboBoxFireMaterial", fireHydantStruct.ComboBoxFireMaterial);
				(xmlNode2 as XmlElement).SetAttribute("comboBoxFireLength", fireHydantStruct.ComboBoxFireLength);
				(xmlNode2 as XmlElement).SetAttribute("comboBoxNozzleAngle", fireHydantStruct.ComboBoxNozzleAngle);
				(xmlNode2 as XmlElement).SetAttribute("checkBox", fireHydantStruct.CheckBox);
				(xmlNode2 as XmlElement).SetAttribute("textBoxWaterLevels", fireHydantStruct.TextBoxWaterLevels);
				xmlNode.AppendChild(xmlNode2);
			}
			xmlDocument.Save(this.path);
		}

		private string path = "";
	}
}
